/**
 * Created by Administrator on 2015/11/23.
 */

var IUser = require("../api/service").IUser
var Page = require('../domain/domain').Page

module.exports = function (app) {

    /**
     * ========首页========
     */
    app.get('/', function *() {
        var recmms = yield this.db('SELECT id,title,finishedProductImage AS image FROM Recipe ORDER BY collect DESC LIMIT 0,4').list()
        var sqls = [
            'SELECT id,title,finishedProductImage AS image, DATE_FORMAT(createOn,"%u") AS week',
            'FROM Recipe',
            'WHERE createOn',
            'ORDER BY week DESC LIMIT 0,4'
        ]
        var coos = yield this.db(sqls.join(' ')).list()

        yield this._render('index', {
            title: "首页",
            recmms: recmms,
            coos: coos
        })
    })

    /**
     * ========== favicon.ico==========
     */
    app.get('/favicon.ico', function *() {
        this.response.redirect('/images/favicon.ico')
    })

    /**
     * ===========菜谱首页============
     */
    app.get(['/recipe', '/recipe/ctg/:cid'], function *() {
        var $self = this
        var ctgs = yield $self.db('SELECT id,name FROM RecipeCtg WHERE parent_id IS NULL').list()
        yield (function *(collects) {
            var callee = arguments.callee
            for(var i=0; i<collects.length; i++){
                var array = yield $self.db('SELECT id,name FROM RecipeCtg WHERE parent_id=?', collects[i].id).list()
                if(array.length > 0){
                    yield callee(array)
                    collects[i].children = array
                }
            }
        })(ctgs)
        var recipes
        var page
        var ctg
        if(!this.params.cid){
            recipes = yield $self.db('SELECT id,title,finishedProductImage,createOn,upvote,comment FROM Recipe ORDER BY upvote ASC LIMIT 0,8').list()
        }else{
            var pageNum = this.query.pageNum || 1
            var cid = this.params.cid
            ctg = yield $self.db('SELECT * FROM RecipeCtg WHERE id=?', this.params.cid).unique()
            var sqls = [
                'SELECT id,title,finishedProductImage,createOn,upvote,comment',
                'FROM Recipe',
                'WHERE category_id=?',
                'ORDER BY createOn ASC',
                'LIMIT ?,?'
            ]
            var count = yield $self.db('SELECT COUNT(*) FROM Recipe WHERE category_id=?', cid).count()
            recipes = yield $self.db(sqls.join(' '), [cid, (pageNum-1)*8, 8]).list()
            page = new Page(pageNum, 8, count, recipes)
        }
        yield this._render('recipe', {
            title: "菜谱",
            ctgs: ctgs,
            recipes: recipes,
            page: page,
            ctg: ctg
        })
    })

    /**
     * ==========菜单首页===========
     */
    app.get('/menu', function *() {
        yield this._render('/menu/menu', {
            title: "菜单"
        })
    })

    /**
     * ==========登录页===========
     */
    app.get('/login', function *() {
        yield this._render('login', {
            title: "登录"
        })
    })

    /**
     * ==========注册页===========
     */
    app.get('/register', function *() {
        yield this._render('register', {
            title: "注册"
        })
    })

    /**
     * ===========菜单详情===========
     */
    app.get('/menuDetail', function *() {
        yield this._render('/menu/menuDetail', {
            title: "菜单详情"
        })
    })

}